package com.baosight.risk.common.quartz;

import com.baosight.risk.api.fltd.FltdProcessService;
import com.baosight.risk.common.aspects.factory.AsyncFactory;
import com.baosight.risk.common.manager.AsyncManager;
import com.baosight.risk.service.system.entity.SysOperLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baosight.risk.api.xtri.IXtriTempActiveService;
import com.baosight.risk.api.xtri.IXtriTempRiskService;
import com.baosight.risk.api.xtri.IXtriTempSystemService;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component("xtriTempTask")
public class XtriTempTask {
    @Autowired
    private IXtriTempSystemService tempSystemService;
    @Autowired
    private IXtriTempActiveService tempActiveService;
    @Autowired
    private IXtriTempRiskService tempRiskService;
    @Autowired
    private FltdProcessService fltdProcessService;
    
    public void importSystem(String params, String jobId)throws Exception {
        List<SysOperLog> logs = new ArrayList<>();
        Exception error = null;
        log.error("进入定时任务————制度文件插入服务接口");
        try {
            tempSystemService.importSystem();
            logs.add(fltdProcessService.getLog("制度文件插入服务接口定时任务执行成功", 0, "成功", jobId));
        }catch (Exception e){
            logs.add(fltdProcessService.getLog("制度文件插入服务接口定时任务执行失败", 1, e.getMessage(), jobId));
            log.error("执行结束："+e.getMessage());
        }
        log.error("执行结束————制度文件插入服务接口");
        AsyncManager.me().execute(AsyncFactory.batchRecordOper(logs));
        if (error != null) {
            throw error;
        }
    }

    public void importActive(String params, String jobId)throws Exception {
        List<SysOperLog> logs = new ArrayList<>();
        Exception error = null;
        log.error("进入定时任务————控制活动插入服务接口");
        try {
            tempActiveService.importActive();
            logs.add(fltdProcessService.getLog("控制活动插入服务接口定时任务执行成功", 0, "成功", jobId));
        }catch (Exception e){
            logs.add(fltdProcessService.getLog("控制活动插入服务接口定时任务执行失败", 1, e.getMessage(), jobId));
            log.error("执行结束："+e.getMessage());
        }
        log.error("执行结束————控制活动插入服务接口");
        AsyncManager.me().execute(AsyncFactory.batchRecordOper(logs));
        if (error != null) {
            throw error;
        }
    }

    public void importRisk(String params, String jobId)throws Exception {
        List<SysOperLog> logs = new ArrayList<>();
        Exception error = null;
        log.error("进入定时任务————风险清单插入服务接口");
        try {
            tempRiskService.importRisk();
            logs.add(fltdProcessService.getLog("风险清单插入服务接口定时任务执行成功", 0, "成功", jobId));
        }catch (Exception e){
            logs.add(fltdProcessService.getLog("风险清单插入服务接口定时任务执行失败", 1, e.getMessage(), jobId));
            log.error("执行结束："+e.getMessage());
        }
        log.error("执行结束————风险清单插入服务接口");
        AsyncManager.me().execute(AsyncFactory.batchRecordOper(logs));
        if (error != null) {
            throw error;
        }
    }
}
